﻿Imports System.Data
Imports System.Data.SqlClient
Imports CAPA_Entidad
Imports CAPA_Utilitarios
Public Class DocumentoAlmacenPickeadorTiempoDatos

    'Se crea una instancia unica para las conexiones
    Dim conex As Conexion = Conexion.getInstance()

    Function registrarDocumentoAlmacenPickeadorTiempo(ByVal dapt As DocumentoAlmacenPickeadorTiempo) As Boolean
        Dim valor As Boolean = False
        Dim con As SqlConnection = conex.obtenerConexion()
        Try
            con.Open()

            Dim query As String = "INSERT INTO Documento_Almacen_Pickeador_Tiempo(CIA,SUC_Codigo,TDOC_Codigo,DCA_Codigo,DAP_Codigo, " & _
                                  "ALM_Codigo,PICK_Codigo,EVE_Codigo,DAPT_FechaIni,DAPT_FechaFin,DAPT_Activo) " & _
                                  "VALUES(@CIA,@SUC,@TDO,@DCA,@DAP,@ALM,@PICK,@EVE,GETDATE(),NULL,'1')"

            Dim comando As New SqlCommand(query, con)
            With comando
                .CommandText = query
                .Parameters.AddWithValue("@CIA", dapt.CIA)
                .Parameters.AddWithValue("@SUC", dapt.SUC_Codigo)
                .Parameters.AddWithValue("@TDO", dapt.TDOC_Codigo)
                .Parameters.AddWithValue("@DCA", dapt.DCA_Codigo)
                .Parameters.AddWithValue("@DAP", dapt.DAP_Codigo)
                .Parameters.AddWithValue("@ALM", dapt.ALM_Codigo)
                .Parameters.AddWithValue("@PICK", dapt.PICK_Codigo)
                .Parameters.AddWithValue("@EVE", dapt.EVE_Codigo)
                .ExecuteNonQuery()
            End With

            valor = True
        Catch ex As Exception
            valor = False
        Finally
            con.Close()
        End Try

        Return valor
    End Function

    Function verificarPedidoPickeadorTiempo(ByVal codDCA As String, ByVal codpick As String, ByVal ppCod As String, ByVal tipoDOC As String) As String
        Dim texto As String = ""
        Dim con As SqlConnection = conex.obtenerConexion()

        Try
            con.Open()

            Dim query As String = "SELECT TOP 1 EVE_Codigo " & _
                                  "FROM Documento_Almacen_Pickeador_Tiempo " & _
                                  "WHERE DAL_Codigo = '" & codDCA & "' " & _
                                  "AND TDOC_Codigo = '" & tipoDOC & "' " & _
                                  "AND PICK_Codigo = '" & codpick & "' " & _
                                  "AND DAP_Codigo = '" & ppCod & "' " & _
                                  "ORDER BY DAPT_Codigo DESC"

            Dim adapter As SqlDataAdapter = New SqlDataAdapter(query, con)
            Dim ds As New DataSet
            adapter.Fill(ds)

            If ds.Tables(0).Rows.Count <> 0 Then
                texto = ds.Tables(0).Rows(0)("EVE_Codigo").ToString.Trim
            End If
        Catch ex As Exception
            texto = "-1"
        Finally
            con.Close()
        End Try

        Return texto
    End Function

    Function actualizarTiempoFinStandBy(ByVal dapt As DocumentoAlmacenPickeadorTiempo) As Boolean
        Dim valor As Boolean = False
        Dim con As SqlConnection = conex.obtenerConexion()

        Try
            con.Open()

            Dim query As String = "UPDATE Documento_Almacen_Pickeador_Tiempo " & _
                                  "SET DAPT_FechaFin = GETDATE() " & _
                                  "WHERE DAPT_Codigo = @DAPT AND CIA = @CIA AND SUC_Codigo = @SUC " & _
                                  "AND TDOC_Codigo = @TDO AND DCA_Codigo = @DCA AND DAP_Codigo = @DAP " & _
                                  "AND DAPT_Activo = '1'"

            Dim comando As New SqlCommand(query, con)
            With comando
                .CommandText = query
                .Parameters.AddWithValue("@DAPT", dapt.DAPT_Codigo)
                .Parameters.AddWithValue("@CIA", dapt.CIA)
                .Parameters.AddWithValue("@SUC", dapt.SUC_Codigo)
                .Parameters.AddWithValue("@TDO", dapt.TDOC_Codigo)
                .Parameters.AddWithValue("@DCA", dapt.DCA_Codigo)
                .Parameters.AddWithValue("@DAP", dapt.DAP_Codigo)
                .ExecuteNonQuery()
            End With

            valor = True
        Catch ex As Exception
            valor = False
        Finally
            con.Close()
        End Try

        Return valor
    End Function

    Function obtenerDocumentoAlmacenPickeadorTiempo(ByVal codCIA As String, ByVal codSUC As String, ByVal tdoCod As String, ByVal codDCA As String, ByVal codDAP As String) As DocumentoAlmacenPickeadorTiempo
        Dim dapt As DocumentoAlmacenPickeadorTiempo = Nothing
        Dim con As SqlConnection = conex.obtenerConexion()
        Try
            con.Open()

            Dim query As String = "SELECT TOP 1 DAPT_Codigo, DAP_Codigo, CIA, SUC_Codigo, TDOC_Codigo, DCA_Codigo, ALM_Codigo, " & _
                                  "PICK_Codigo, EVE_Codigo, DAPT_FechaIni, DAPT_FechaFin, DAPT_Activo " & _
                                  "FROM Documento_Almacen_Pickeador_Tiempo " & _
                                  "WHERE CIA = @CIA AND SUC_Codigo = @SUC AND TDOC_Codigo = @TDO AND DCA_Codigo = @DCA " & _
                                  "AND DAP_Codigo = @DAP AND DAPT_Activo = '1' " & _
                                  "ORDER BY DAPT_Codigo DESC"

            Dim comando As New SqlCommand(query, con)
            With comando
                .Parameters.AddWithValue("@CIA", codCIA)
                .Parameters.AddWithValue("@SUC", codSUC)
                .Parameters.AddWithValue("@TDO", tdoCod)
                .Parameters.AddWithValue("@DCA", codDCA)
                .Parameters.AddWithValue("@DAP", codDAP)
            End With
            Dim adapter As New SqlDataAdapter(comando)
            Dim ds As New DataSet
            adapter.Fill(ds)

            If ds.Tables(0).Rows.Count > 0 Then
                dapt = New DocumentoAlmacenPickeadorTiempo(ds.Tables(0).Rows(0)("DAPT_Codigo").ToString.Trim, ds.Tables(0).Rows(0)("CIA").ToString.Trim, ds.Tables(0).Rows(0)("SUC_Codigo").ToString.Trim, ds.Tables(0).Rows(0)("TDOC_Codigo").ToString.Trim, _
                                                           ds.Tables(0).Rows(0)("DCA_Codigo").ToString.Trim, ds.Tables(0).Rows(0)("DAP_Codigo").ToString.Trim, ds.Tables(0).Rows(0)("ALM_Codigo").ToString.Trim, ds.Tables(0).Rows(0)("PICK_Codigo").ToString.Trim, _
                                                           ds.Tables(0).Rows(0)("EVE_Codigo").ToString.Trim, ds.Tables(0).Rows(0)("DAPT_FechaIni").ToString.Trim, ds.Tables(0).Rows(0)("DAPT_FechaFin").ToString.Trim, ds.Tables(0).Rows(0)("DAPT_Activo").ToString.Trim)
            Else
                dapt = New DocumentoAlmacenPickeadorTiempo("-1", ds.Tables(0).Rows(0)("CIA").ToString.Trim, ds.Tables(0).Rows(0)("SUC_Codigo").ToString.Trim, ds.Tables(0).Rows(0)("TDOC_Codigo").ToString.Trim, _
                                                           ds.Tables(0).Rows(0)("DCA_Codigo").ToString.Trim, ds.Tables(0).Rows(0)("DAP_Codigo").ToString.Trim, ds.Tables(0).Rows(0)("ALM_Codigo").ToString.Trim, ds.Tables(0).Rows(0)("PICK_Codigo").ToString.Trim, _
                                                           ds.Tables(0).Rows(0)("EVE_Codigo").ToString.Trim, ds.Tables(0).Rows(0)("DAPT_FechaIni").ToString.Trim, ds.Tables(0).Rows(0)("DAPT_FechaFin").ToString.Trim, ds.Tables(0).Rows(0)("DAPT_Activo").ToString.Trim)
            End If
        Catch ex As Exception
            dapt = Nothing
        Finally
            con.Close()
        End Try

        Return dapt
    End Function

End Class
